package com.step.sprite;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import com.step.Step;
import com.step.Step.Dimension;
import com.step.graphic.Texture;
import com.step.math.Vector;
import com.step.mesh.Mesh;

public class Sprite3D extends Sprite{
	protected boolean animationEnable = false;
	
	protected ArrayList<Sprite3D> children;
	
	public Sprite3D(Mesh mesh, Texture texture){
		super(mesh, texture);
		position = new Vector(Dimension.d3d);
		scale = new Vector(1, 1, 1);
		rotation = new Vector(Dimension.d3d);
	}
	
	public Sprite3D(Texture texture){
		super(null, texture);
		position = new Vector(Dimension.d3d);
		scale = new Vector(1, 1, 1);
		rotation = new Vector(Dimension.d3d);
		
		Mesh mesh = new Mesh(Dimension.d3d, 24, 12, true,true,false);
		// 前面
		mesh.addVertex(-0.5f, -0.5f, 0.5f, 255, 0, 0, 255, 0, 1);
		mesh.addVertex( 0.5f, -0.5f, 0.5f, 255, 0, 0, 255, 1, 1);
		mesh.addVertex( 0.5f,  0.5f, 0.5f, 255, 0, 0, 255, 1, 0);
		mesh.addVertex(-0.5f,  0.5f, 0.5f, 255, 0, 0, 255, 0, 0);

		// 右面
		mesh.addVertex(0.5f, -0.5f,  0.5f, 0, 255, 0, 255, 0, 1);
		mesh.addVertex(0.5f, -0.5f, -0.5f, 0, 255, 0, 255, 1, 1);
		mesh.addVertex(0.5f,  0.5f, -0.5f, 0, 255, 0, 255, 1, 0);
		mesh.addVertex(0.5f,  0.5f,  0.5f, 0, 255, 0, 255, 0, 0);

		// 后面
		mesh.addVertex( 0.5f, -0.5f, -0.5f, 0, 0, 255, 255, 0, 1);
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 0, 0, 255, 255, 1, 1);
		mesh.addVertex(-0.5f,  0.5f, -0.5f, 0, 0, 255, 255, 1, 0);
		mesh.addVertex( 0.5f,  0.5f, -0.5f, 0, 0, 255, 255, 0, 0);

		// 左面
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 255, 0, 255, 255, 0, 1);
		mesh.addVertex(-0.5f, -0.5f,  0.5f, 255, 0, 255, 255, 1, 1);
		mesh.addVertex(-0.5f,  0.5f,  0.5f, 255, 0, 255, 255, 1, 0);
		mesh.addVertex(-0.5f,  0.5f, -0.5f, 255, 0, 255, 255, 0, 0);

		//  上面
		mesh.addVertex(-0.5f, 0.5f,  0.5f, 255, 255, 0, 255, 0, 1);
		mesh.addVertex( 0.5f, 0.5f,  0.5f, 255, 255, 0, 255, 1, 1);
		mesh.addVertex( 0.5f, 0.5f, -0.5f, 255, 255, 0, 255, 1, 0);
		mesh.addVertex(-0.5f, 0.5f, -0.5f, 255, 255, 0, 255, 0, 0);

		// 下面
		mesh.addVertex(-0.5f, -0.5f,  0.5f, 0, 255, 255, 255, 0, 1);
		mesh.addVertex( 0.5f, -0.5f,  0.5f, 0, 255, 255, 255, 1, 1);
		mesh.addVertex( 0.5f, -0.5f, -0.5f, 0, 255, 255, 255, 1, 0);
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 0, 255, 255, 255, 0, 0);
		
		mesh.addFace(0, 1, 3);
		mesh.addFace(1, 2, 3);
		mesh.addFace(4, 5, 7);
		mesh.addFace(5, 6, 7);
		mesh.addFace(8, 9, 11);
		mesh.addFace(9, 10, 11);
		mesh.addFace(12, 13, 15);
		mesh.addFace(13, 14, 15);
		mesh.addFace(16, 17, 19);
		mesh.addFace(17, 18, 19);
		mesh.addFace(20, 21, 23);
		mesh.addFace(21, 22, 23);
		setMesh(mesh);
	}
	
	public void draw(GL10 gl){
		draw(gl, RenderType.TRIANGLES);
	}
	
	@Override
	public void draw(GL10 gl, RenderType renderType) {
		super.draw(gl,renderType);
		
		if(children != null && children.size() > 0){
			int len = children.size();
			for (int i = 0; i < len; i++) {
				children.get(i).draw(gl,renderType);
			}
		}		
	}

	public void dispose(){
		if(mesh != null) mesh.dispose();
		if(parent != null) parent.remove(this);
	}

	@Override
	public boolean is2D() {
		return false;
	}

	@Override
	public boolean is3D() {
		return true;
	}

	public Vector getPosition() {
		return position;
	}

	public void setPosition(float x, float y, float z){
		position.set(x, y, z);
	}
	
	public void setPosition(Vector position){
		this.position.set(position);
	}
	
	public void setRotation(float angleX, float angleY, float angleZ){
		rotation.set(angleX, angleY, angleZ);
	}
	
	public void setRotation(Vector angleXYZ){
		rotation.set(angleXYZ);
	}
	
	public void rotate(float angleX, float angleY, float angleZ){
		rotation.add(angleX, angleY, angleZ);
	}
	
	public void rotate(float angle){
		Step.log("Sprite3D-旋转前："+super.rotation.toString());
		rotation.add(angle, angle, angle);
		Step.log("Sprite3D-旋转后："+super.rotation.toString());
	}
	
	public Vector getRotation() {
		return rotation;
	}
	
	public void setScale(float scaleX, float scaleY, float scaleZ){
		scale.set(scaleX, scaleY, scaleZ);
	}
	
	public void setScale(Vector scale){
		this.scale.set(scale);
	}
	
	public void scale(float scaleX, float scaleY, float scaleZ){
		this.scale.add(scaleX, scaleY, scaleZ);
	}
	
	public void scale(Vector scale){
		this.scale.add(scale);
	}
	
	public Vector getScale() {
		return scale;
	}
	
	public Sprite3D clone(){
		return new Sprite3D(mesh.clone(), texture);
	}
}
